package controllers;

import helpers.QuestionCrawlerHelper;
import helpers.StringHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import systems.ConfigSystem;
import systems.RecentlyQuestion;

import admin.models.CategoryModel;
import admin.models.MetaSeoModel;

import com.google.gson.Gson;

import debugs.QuestionDebug;

import entities.Answer;
import entities.Author;
import entities.Category;
import entities.MetaSeo;
import entities.Question;
import entities.SiteConfig;

import models.AuthorModel;
import models.QuestionModel;

public class QuestionInfoServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		SiteConfig config = ConfigSystem.getConfigs();
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = path.replaceAll(request.getServletPath() + "/", "");		
		
		if (id == null || id.isEmpty()) {
			response.sendRedirect("/");
			return;
		}
		
		String url = URLDecoder.decode(id);
		
		QuestionModel questionModel = QuestionModel.getInstance();
		Question question = questionModel.getQuestion(id);
		
		if (question == null && url.isEmpty()) {
			response.sendRedirect("/");
			return ;
		} else if (question == null) {
			question = QuestionCrawlerHelper.getQuestion(url, response);
		}
		
		if (question == null) {
			response.sendRedirect("/");
			return ;
		}
		
		
		CategoryModel categoryModel = CategoryModel.getInstance();
		Category category = categoryModel.getCategory(question.getCategory());
		
		List<String> ids = new ArrayList<String>();
		
		if (!question.getAuthor().isEmpty()) {
			ids.add(question.getAuthor());
		}
		
		List<Answer> answers = new ArrayList<Answer>();
		if (!question.getAnswers().isEmpty()) {
			for (String ans : question.getAnswers()) {
				Answer answer = new Gson().fromJson(ans, Answer.class);
				answers.add(answer);
				if (!answer.getAuthor().isEmpty()) {
					ids.add(answer.getAuthor());
				}
			}
		}
		
		AuthorModel authorModel = AuthorModel.getInstance();
		Map<String, Author> authors = authorModel.getByListIds(ids);
		
		RecentlyQuestion.insert(question);
		
		//Set seo content
		MetaSeoModel metaseoModel = MetaSeoModel.getInstance();
		MetaSeo metaseo = metaseoModel.get("question");
		
		/*//Cache header
		response.addHeader("Cache-Control", "public");
		response.addHeader("Cache-Control", "max-age=" + 60000);*/
		
		request.setAttribute("question", question);
		request.setAttribute("authors", authors);
		request.setAttribute("category", category);
		request.setAttribute("answers", answers);
		request.setAttribute("metaseo", metaseo);
		try {
			request.getRequestDispatcher("/templates/" + config.getTemplate() + "/pages/question_info.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
